<?php

/**
 * 默认控制器
 */
class Controller_Behaviorlog extends Controller_Abstract
{
    /* ******************************************************************** */
    function actionOutput()
    {
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$show_box['title'] = '查询详细行为日志';

		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/hostlist'));

		if(isset($this->_context->mac)){
			$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
		}

		if(isset($this->_context->id)){
			$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
		}
    }

	/* ******************************************************************** */
    function actionTimecheck()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$show_box['title'] = '查询详细行为日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeHosts(url('default::behaviorlog/hostlist'));

		if(isset($this->_context->mac)){
			$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
		}

		if(isset($this->_context->id)){
			$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
		}
		//dump(Q::cache('rightitem'));
		if(Q::cache('rightitem')!=''){
			//重置下拉菜单
			$this->_view['form']->add(QForm::ELEMENT, 'groupid', array('_ui' => 'hidden','value' => Q::cache('rightitem')));
		}
    }

	function actionOutputs()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '查询详细行为日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/hostlist'));

		if(isset($this->_context->mac)){
			$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
		}

		if(isset($this->_context->id)){
			$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
		}
		//查询，导出和批量删除
        //dump($_SESSION);
    }

    /* ******************************************************************** */
    function actionIndex()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '行为日志首页';
		$iftype='';$skey='';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
			
			//$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/index'));
			$this->_view['form'] = new Form_BeFliter(url('default::behaviorlog/index', array('mac' => $this->_context->mac,'startd' => $this->_context->startd,'endd' => $this->_context->endd)));

			if(isset($this->_context->mac)){
				$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
			}

			//$autoinput=array('action' => '','process' => 'PID','fileinfo' => '','tcpconns' => 'originalip|originalport|targetip|targetport|filename|state','cmdinfo' => '','terminallog' => 'loginIP|loginname|loginuser|loginmac','reginfo' => '');

			$this->_view['sk']='';
			$this->_view['nowselect']='';
			//时间筛选
			$addconds="date = '".date("Y-m-d")."'";$cstr='';$sdstr='';$edstr='';$tstr='';
			$ostr='id';$osstr='desc';$edit_one=array();
			$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
			$this->_view['startd']='';$otherstr='';
			$this->_view['endd']='';
			$this->_view['macs']='';
			$this->_view['PID']='';
			$this->_view['ordercol']=' id ';
			$this->_view['contents']='';
			$this->_view['originalip']='';$this->_view['originalport']='';
			$this->_view['targetip']='';$this->_view['targetport']='';
			$this->_view['filename']='';$this->_view['state']='';
			
			$this->_view['loginIP']='';$this->_view['loginname']='';
			$this->_view['loginuser']='';$this->_view['loginmac']='';
			$pagesize = 10;
			$addmac='';$addcontent='';$addkeys='';
			$edit_one=array();$others=array();
			if ($this->_context->isPOST())
			{
				
				if(isset($_POST['perpage']) && $_POST['perpage']!=''){
					$pagesize = $_POST['perpage'];
					$others[]='per='.$_POST['perpage'];
				}
				
				$edit_one['infotype']=$_POST['infotype'];
				$edit_one['content']=$_POST['content'];
				$edit_one['ordercol']=$_POST['ordercol'];
				$edit_one['desc']=$_POST['desc'];
				$this->_view['form']->import($edit_one);
				//$this->_view['form']['infotype']=$_POST['infotype'];
				if(isset($_POST['enddate']) && isset($_POST['startdate'])){
				
					if($_POST['startdate']==$_POST['enddate']){
						//$endd=$this->my_date_add('d',$_POST['enddate'],1);
					}else{
						//$endd=$_POST['enddate'];
					}
					$endd=$_POST['enddate'];
					$this->_view['startd']=$_POST['startdate'];
					$this->_view['endd']=$_POST['enddate'];
				}
				if(isset($_POST['mac'])){
					$this->_view['macs']=$_POST['mac'];
					$addmac=" and macaddress='".$_POST['mac']."' ";
				}

				if($_POST['startdate']!='')
					$sdstr='&startd='.$_POST['startdate'].'';

				if($_POST['enddate']!='')
					$edstr='&endd='.$_POST['enddate'].'';

				//类型对应数据表
				if(isset($_POST['infotype']) || isset($this->_context->infotype)){
					$iftype=$_POST['infotype'];
					
					if(isset($this->_context->infotype)){
						$iftype=$this->_context->infotype;
						//echo $iftype;
					}

					switch ($iftype)
					{
					case '':
					  $skey='content';break;
					case 'process':
					  $skey='procname';break;
					case 'fileinfo':
					  $skey='filename';break;
					case 'tcpconns':
					  $skey='process';break;
					case 'cmdinfo':
					 $skey='detail';break;
					case 'terminallog':
					  $skey='loginip';break;
					case 'reginfo':
					  $skey='changekey';break;
					case 'reginfo1':
					  $skey='changekey';break;
					default:
					  $skey='content';break;
					}
					$this->_view['sk']=$skey;
					$this->_view['nowselect']=$iftype;
					
					$this->_view['form']->add(QForm::ELEMENT, 'infot', array('_ui' => 'hidden','value' => $skey));
					
					//内容关键字
					if($_POST['content']!=''){
						$this->_view['contents']=$_POST['content'];
						//如果有空格
						$c=explode(' ',$_POST['content']);
						if(count($c)>1){  
							$alln=array();
							for($i=0; $i < count($c); $i++) {
								if($c[$i]!='')
									$alln[]=" ".$skey." like '%".str_replace('\\','\\\\',addslashes($c[$i]))."%' ";
							}
							$addcontent.=" and (".join(' and ',$alln).") ";
						}else{
							$addcontent.=" and ".$skey." like '%".str_replace('\\','\\\\',addslashes($_POST['content']))."%' ";
						}
						//$addcontent=" and ".$skey." like '%".$_POST['content']."%' ";
						$cstr='&keys='.urlencode($_POST['content']).'';
					}
				}else{
				
					//内容关键字
					if($_POST['content']!=''){
						$this->_view['contents']=$_POST['content'];
						$addcontent=" and content like '%".$_POST['content']."%' ";
						
						$cstr='&keys='.$_POST['content'].'';
					}

					$this->_view['sk']='';
				
				}


				if(isset($_POST['enddate']) && isset($_POST['startdate'])){
					//$addconds="date >='".$_POST['startdate']."' and date<='".$endd."'".$addcontent.$addmac;
					$addconds="date >='".$_POST['startdate']." 00:00:00' and date<='".$endd." 23:59:59'".$addcontent.$addkeys;
				}else{
					$addconds="date >='".date("Y-m-d")." 00:00:00' and date<='".date("Y-m-d")." 23:59:59'".$addcontent.$addkeys;
				}
				
				//进程信息PID关键字
				if(isset($_POST['PID']) && $_POST['PID']!=''){
					$this->_view['PID']=$_POST['PID'];
					$addcontent.=" and PID = ".$_POST['PID']." ";
					$others[]='pid='.$_POST['PID'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息originalip关键字
				if(isset($_POST['originalip']) && $_POST['originalip']!=''){
					$this->_view['originalip']=$_POST['originalip'];
					$addcontent.=" and originalip = '".$_POST['originalip']."' ";
					$others[]='originalip='.$_POST['originalip'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息originalport关键字
				if(isset($_POST['originalport']) && $_POST['originalport']!=''){
					$this->_view['originalport']=$_POST['originalport'];
					$addcontent.=" and originalport = ".$_POST['originalport']." ";
					$others[]='originalport='.$_POST['originalport'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息targetip关键字
				if(isset($_POST['targetip']) && $_POST['targetip']!=''){
					$this->_view['targetip']=$_POST['targetip'];
					$addcontent.=" and targetip = ".$_POST['targetip']." ";
					$others[]='targetip='.$_POST['targetip'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息targetport关键字
				if(isset($_POST['targetport']) && $_POST['targetport']!=''){
					$this->_view['targetport']=$_POST['targetport'];
					$addcontent.=" and targetport = ".$_POST['targetport']." ";
					$others[]='targetport='.$_POST['targetport'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息filename关键字
				if(isset($_POST['filename']) && $_POST['filename']!=''){
					$this->_view['filename']=$_POST['filename'];
					$addcontent.=" and filename like '%".$_POST['filename']."%' ";;
					$others[]='filename='.$_POST['filename'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//连接信息state关键字
				if(isset($_POST['state']) && $_POST['state']!=''){
					$this->_view['state']=$_POST['state'];
					$addcontent.=" and state = '".$_POST['state']."' ";
					$others[]='state='.urlencode($_POST['state']);
					//$cstr='&keys='.$_POST['content'].'';
				}

				//远程登录信息loginIP关键字
				if(isset($_POST['loginIP']) && $_POST['loginIP']!=''){
					$this->_view['loginIP']=$_POST['loginIP'];
					$addcontent.=" and loginIP = ".$_POST['loginIP']." ";
					$others[]='loginIP='.$_POST['loginIP'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//远程登录信息loginname关键字
				if(isset($_POST['loginname']) && $_POST['loginname']!=''){
					$this->_view['loginname']=$_POST['loginname'];
					$addcontent.=" and loginname = ".$_POST['loginname']." ";
					$others[]='loginname='.$_POST['loginname'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//远程登录信息loginuser关键字
				if(isset($_POST['loginuser']) && $_POST['loginuser']!=''){
					$this->_view['loginuser']=$_POST['loginuser'];
					$addcontent.=" and loginuser = ".$_POST['loginuser']." ";
					$others[]='loginuser='.$_POST['loginuser'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//远程登录信息loginmac关键字
				if(isset($_POST['loginmac']) && $_POST['loginmac']!=''){
					$this->_view['loginmac']=$_POST['loginmac'];
					$addcontent.=" and loginmac = ".$_POST['loginmac']." ";
					$others[]='loginmac='.$_POST['loginmac'];
					//$cstr='&keys='.$_POST['content'].'';
				}

				//order排序关键字
				if($_POST['ordercol']!='' && $_POST['desc']!=''){
					$this->_view['ordercol']=$_POST['ordercol'];
					$this->_view['desc']=$_POST['desc'];
					//$addcontent.=" and szdesc like '%".$_POST['szdesc']."%' ";
					$ostr=$_POST['ordercol'];
					$osstr=$_POST['desc'];
					$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
					//$others[]='ordercol='.$_POST['ordercol'];
					//$others[]='desc='.$_POST['desc'];
				}else{
				
					$this->_view['ordercol']='id';
					$this->_view['desc']=$_POST['desc'];
					$ostr='id';
					$osstr=$_POST['desc'];
					$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
				}
				$otherstr= '&'.join("&",$others);
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//dump($addconds);
				//dump($_POST);
			}
			
			if(isset($this->_context->infotype)){
				$iftype=$this->_context->infotype;
				$this->_view['nowselect']=$iftype;
				switch ($iftype)
					{
					case '':
					  $skey='content';break;
					case 'process':
					  $skey='procname';break;
					case 'fileinfo':
					  $skey='filename';break;
					case 'tcpconns':
					  $skey='process';break;
					case 'cmdinfo':
					 $skey='detail';break;
					case 'terminallog':
					  $skey='loginip';break;
					case 'reginfo':
					  $skey='changekey';break;
					case 'reginfo1':
					  $skey='changekey';break;
					default:
					  $skey='content';break;
					}
					$this->_view['sk']=$skey;
				//echo $iftype;
			}else{
				$skey='content';
			}

			
			if($this->_context->startd !='' && $this->_context->endd !='' && !$this->_context->isPOST()){
				//echo $this->_context->macs;
				
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				
				if($this->_context->macs!=''){
					$addmac=" and macaddress='".$this->_context->macs."' ";
				}

				if($this->_context->keys!=''){
					//如果有空格
						$c=explode(' ',$this->_context->keys);
						if(count($c)>1){  
							$alln=array();
							for($i=0; $i < count($c); $i++) {
								if($c[$i]!='')
									$alln[]=" ".$skey." like '%".str_replace('\\','\\\\',addslashes($c[$i]))."%' ";
							}
							$addkeys=" and (".join(' and ',$alln).") ";
						}else{
							$addkeys=" and ".$skey." like '%".str_replace('\\','\\\\',addslashes($this->_context->keys))."%' ";
						}
					//$addkeys=" and ".$skey." like '%".$this->_context->keys."%' ";
				}

				//$addconds="date >='".$this->_context->startd."' and date<='".$this->_context->endd."'".$addmac;
				$addconds=" (date >='".$this->_context->startd."' and date<='".$this->_context->endd."') ".$addcontent.$addkeys;
				
				if($this->_context->startd!='')
					$sdstr='&startd='.$this->_context->startd.'';

				if($this->_context->startd!='')
					$edstr='&endd='.$this->_context->endd.'';
				
			}
			//echo $skey;
			if($this->_context->infotype!='')
					$tstr='&infotype='.$this->_context->infotype.'';

			if($this->_context->keys!='')
					$cstr='&keys='.$this->_context->keys.'';

			if($this->_context->pid!=''){
				$addconds.=" and pid = ".$this->_context->pid." ";
				$others[]='pid='.$this->_context->pid;
				$this->_view['pid']=$this->_context->pid;
			}

			if($this->_context->originalip!=''){
				$addconds.=" and originalip = ".$this->_context->originalip." ";
				$others[]='originalip='.$this->_context->originalip;
				$this->_view['originalip']=$this->_context->originalip;
			}

			if($this->_context->originalport!=''){
				$addconds.=" and originalport = ".$this->_context->originalport." ";
				$others[]='originalport='.$this->_context->originalport;
				$this->_view['originalport']=$this->_context->originalport;
			}

			if($this->_context->targetip!=''){
				$addconds.=" and targetip = ".$this->_context->targetip." ";
				$others[]='targetip='.$this->_context->targetip;
				$this->_view['targetip']=$this->_context->targetip;
			}

			if($this->_context->targetport!=''){
				$addconds.=" and targetport = ".$this->_context->targetport." ";
				$others[]='targetport='.$this->_context->targetport;
				$this->_view['targetport']=$this->_context->targetport;
			}

			if($this->_context->filename!=''){
				$addconds.=" and filename  like '%".$this->_context->filename."%' ";
				$others[]='filename='.$this->_context->filename;
				$this->_view['filename']=$this->_context->filename;
			}

			if($this->_context->state!=''){
				$addconds.=" and state = '".$this->_context->state."' ";
				$others[]='state='.urlencode($this->_context->state);
				$this->_view['state']=$this->_context->state;
			}

			//2342

			if($this->_context->loginIP!=''){
				$addconds.=" and loginIP = ".$this->_context->loginIP." ";
				$others[]='loginIP='.$this->_context->loginIP;
				$this->_view['loginIP']=$this->_context->loginIP;
			}

			if($this->_context->loginname!=''){
				$addconds.=" and loginname = ".$this->_context->loginname." ";
				$others[]='loginname='.$this->_context->loginname;
				$this->_view['loginname']=$this->_context->loginname;
			}

			if($this->_context->loginuser!=''){
				$addconds.=" and loginuser = ".$this->_context->loginuser." ";
				$others[]='loginuser='.$this->_context->loginuser;
				$this->_view['loginuser']=$this->_context->loginuser;
			}

			if($this->_context->loginmac!=''){
				$addconds.=" and loginmac = ".$this->_context->loginmac." ";
				$others[]='loginmac='.$this->_context->loginmac;
				$this->_view['loginmac']=$this->_context->loginmac;
			}


			if (!$this->_context->isPOST()){
				$edit_one['infotype']=$this->_context->infotype;
				$edit_one['content']=$this->_context->content;
				$edit_one['ordercol']=$this->_context->ordercol;
				$this->_view['ordercol']=$this->_context->ordercol;
				$edit_one['desc']=$this->_context->desc;
				$this->_view['form']->import($edit_one);
				$ostr=$this->_context->ordercol;
				$osstr=$this->_context->desc;
				$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
				
			}
			
			

			if(isset($this->_context->per) && $this->_context->per!=''){
				$pagesize = $this->_context->per;
			}

			$otherstr= '&'.join("&",$others);
			$chinesestr='-';
			if($this->_context->mac==''){

				
				// 获取当前页数，通常为get方式参数
				$page = $this->_context->page;
				$this->_view['pageno']=$page;
				
				switch ($iftype)
				{
				case '':
				  $select = OldAction::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'process':
				  $select = Process::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'fileinfo':
				  $select = FileInfo::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'tcpconns':
				  $select = TCPConns::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'cmdinfo':
				  $select = CMDInfo::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'terminallog':
				  $select = Terminallog::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'reginfo':
				  $select = RegInfo::find($addconds)->limitPage($page, $pagesize);
				  break;
				case 'reginfo1':
				  $select = RegInfo::find($addconds)->limitPage($page, $pagesize);
				  break;
				default:
				  $select = OldAction::find($addconds)->limitPage($page, $pagesize);
				}

				//$select = OldAction::find($addconds)->limitPage($page, $pagesize);
				$this->_view['pager'] = $select->getPagination();

				if($ostr." ".$osstr==' '){
					$ostr='id';$osstr='desc';
				}
				$this->_view['group'] = $select->order($ostr." ".$osstr)->getAll();

				if($this->_view['pager']['record_count']>0 && $page=='')
					$page=1;
				//echo $this->_view['pager']['record_count'];
				$pagediv = new Helper_MyPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'index.php?controller=behaviorlog'.$sdstr.$edstr.$tstr.$ostrs.$cstr.$otherstr.'&page=','',5);
				$this->_view['nowperpage']=$pagesize;
				$this->_view['pagestr']= $pagediv->show();

			}else{
			 

				//$pagesize = 10;
				$amac='';
				// 获取当前页数，通常为get方式参数
				$page = $this->_context->page;
				$this->_view['pageno']=$page;
				$this->_view['macs']=$this->_context->mac;
				if(isset($_POST['mac']) && $_POST['mac']!=''){
					$amac=' and macaddress = ?';
					$pmac=$_POST['mac'];
				}
				if($this->_context->mac!=''){
					$amac=' and macaddress = ?';
					$pmac=$this->_context->mac;
				}

				$allconditions=$addconds.$amac;
				$allconditions=iconv("UTF-8","GB2312",$allconditions);
				switch ($iftype)
				{
				case '':
				  $select = OldAction::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'process':
				  $select = Process::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'fileinfo':
				  $select = FileInfo::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'tcpconns':
				  $select = TCPConns::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'cmdinfo':
				  $select = CMDInfo::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'terminallog':
				  $select = Terminallog::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'reginfo':
				  $select = RegInfo::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				case 'reginfo1':
				  $select = RegInfo::find($allconditions,$pmac)->limitPage($page, $pagesize);
				  break;
				default:
				  $select = OldAction::find($allconditions,$pmac)->limitPage($page, $pagesize);
				}
				
				//$select = OldAction::find('macaddress = ? AND '.$addconds, $this->_context->mac)->limitPage($page, $pagesize);
				$this->_view['pager'] = $select->getPagination();

				if($ostr." ".$osstr==' '){
					$ostr='id';$osstr='desc';
				}
				$this->_view['group'] = $select->order($ostr." ".$osstr)->getAll();

				if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
				//echo $this->_view['pager']['record_count'];
				$pagediv = new Helper_MyPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'index.php?controller=behaviorlog'.$sdstr.$edstr.$tstr.$cstr.$ostrs.$otherstr.'&mac='.$pmac.'&page=','',5);
				$this->_view['nowperpage']=$pagesize;
				$this->_view['pagestr']= $pagediv->show();

			}
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionDetails(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '行为日志-详细内容';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

		$this->_view['sk'] = $this->_context->tablename;
		$this->_view['theno'] = $this->_context->tableid;
		$iftype=$this->_context->tablename;
		$theno=$this->_context->tableid;
		$tclass='noramltable';
		switch ($iftype)
		{
				case 'PROCESSINFO':
				  $select = Process::find('id =?',$theno);
				  break;
				case 'FILEINFO':
				  $select = FileInfo::find('id =?',$theno);
				  break;
				case 'TCPCONNECT':
				  $select = TCPConns::find('id =?',$theno);
				  break;
				case 'CMDINFO':
				  $select = CMDInfo::find('id =?',$theno);
					$tclass='noramltable1';
				  break;
				case 'TERMINAL':
				  $select = Terminallog::find('id =?',$theno);
				  break;
				case 'REGINFO':
				  $select = Reginfo::find('id =?',$theno);
				  break;
				default:
				  $select = OldAction::find('id =?',$theno);
		}

		//上下条
		$this->_view['prev']=$theno;$this->_view['next']=$theno;
		$table =QDB::getConn();
		$sql = "SELECT id FROM `eventlog` WHERE id < ".$theno." ORDER BY id DESC LIMIT 1";
		$handel =$table->execute($sql);
		$temp2 = $handel->fetchAll();
		foreach ($temp2 as $s){
			if(isset($s['id']))
				$this->_view['prev']=$s['id'];
			else
				$this->_view['prev']=$theno;
		}

		$sql = "SELECT id FROM `eventlog` WHERE id > ".$theno." ORDER BY id ASC LIMIT 1";
		$handel =$table->execute($sql);
		$temp2 = $handel->fetchAll();
		foreach ($temp2 as $s){
			if($s['id'])
				$this->_view['next']=$s['id'];
			else
				$this->_view['next']=$theno;
		}

		$this->_view['nowclass'] =$tclass;
		$this->_view['group'] = $select->order("id desc")->getAll();
	}

	function actionList(){
		
		$show_box['title'] = '行为日志首页';
		
		if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

						 //区分用户
			 $os = array("admin");

		     $aroles = array();
		     $aroles =split('[,]', $_SESSION['acl_task_userdata']['roles']);
			//dump($aroles);
			$isrightp=false;
			 foreach ($aroles as $value){
				if (in_array(strtolower($value), $os)) {
					$isrightp=true;
					//echo 'opip';
					break;
					//echo "Got Irix";
				}
			 }
			 //echo $isrightp;
			 //echo $_SESSION['acl_task_userdata']['user_id'];
			 $adming='';
			 if(!$isrightp){
				$havegroups = array();
				$select1 = Groupuser::find('userid = ?',$_SESSION['acl_task_userdata']['user_id'])->getAll();
				foreach ($select1 as $av){
					array_push($havegroups,$av->groupid);
					//echo $av->groupid;
				}
				$addconds='';
				//dump($havegroups);
				$hgs=count($havegroups);
				$this->_view['adming']=$hgs;
				if($hgs>0){
					$adming=' id in ('.join(",",$havegroups).') ';
				}else{
					$adming=' id in (-1) ';
				}
			 }else{
				
				$this->_view['adming']=-1;
			 }
			 //END

			$table =QDB::getConn();
			$sql = "SELECT DISTINCT groupid FROM `grouptohost`";
			$handel =$table->execute($sql);
			$temp = $handel->fetchAll();
			$allhave = array();
			foreach ($temp as $s){
				$allgroup = array();
				$allgroup['groupid']=$s['groupid'];
				$allhave[$s['groupid']]=$allgroup;
				//echo $s['groupid'];
			}

			//$table =QDB::getConn();
			$sql = "SELECT count( 1 ) AS grouphosts, groupid FROM `grouptohost` GROUP BY `grouptohost`.groupid";
			$handel =$table->execute($sql);
			$temp1 = $handel->fetchAll();
			//$allhosts = array();
			foreach ($temp1 as $s){
				//$allgroup = array();
				//$allgroup['groupid']=$s['groupid'];
				$allhave[$s['groupid']]['hosts']=$s['grouphosts'];
				//echo $s['groupid'];
			}

			//SELECT systeminfo.`macaddress` , hosttable.`id` , systeminfo.`lasttime`
	//		FROM systeminfo
	//		LEFT JOIN hosttable ON hosttable.`macaddress` = systeminfo.`macaddress`
	//		WHERE systeminfo.`lasttime` >= DATE_sub( NOW( ) , INTERVAL 10
	//		MINUTE )
	//		AND systeminfo.`lasttime` < DATE_ADD( NOW( ) , INTERVAL 10
	//		MINUTE )
			//查询据现在20分钟内的主机
			$sql = "SELECT systeminfo.`macaddress`,hosttable.`id`,systeminfo.`lasttime` FROM systeminfo left join hosttable on hosttable.`macaddress` =systeminfo.`macaddress` WHERE systeminfo.`lasttime` >= DATE_sub( NOW( ) , INTERVAL 30 SECOND ) and systeminfo.`lasttime` < DATE_ADD( NOW( ) , INTERVAL 30 SECOND)";
			$handel =$table->execute($sql);
			$temp2 = $handel->fetchAll();
			$achosts = array();
			foreach ($temp2 as $s){
				if(isset($s['id']))
					array_push($achosts,$s['id']);
			}
			$allh=0;$alllivenum=0;
			if(count($achosts)>0){
				$allhaves=0;
				$allh=count($achosts);
				//统计每个组的激活数
				$sql = "SELECT count(1) as grouplives, groupid FROM `grouptohost` WHERE `hostid` IN (".join(",",$achosts).") GROUP BY groupid";
				$handel =$table->execute($sql);
				$temp3 = $handel->fetchAll();
				foreach ($temp3 as $s){
					$allhaves++;
					$allhave[$s['groupid']]['lives']=$s['grouplives'];
					$alllivenum=$s['grouplives']+$alllivenum;
				}
				
			}

			//dump($alllivenum);

			$sql = "SELECT count(`hosttable`.`id`) as allblanks  FROM `logdata`.`hosttable`  LEFT JOIN `grouptohost` ON (grouptohost.hostid = hosttable.id)   WHERE ((grouptohost.groupid is null))";
			$hande9 =$table->execute($sql);
			$temp9 = $hande9->fetchAll();
			foreach ($temp9 as $s){
					$blankcounts=$s['allblanks'];
			}
			
			//print_r($blankcounts);
			$this->_view['blanklives']= $allh-$alllivenum;
			$this->_view['blankalls']= $blankcounts;
			$this->_view['groupc']= $allhave;
			
			

			//$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/hostlist'));

			if(isset($this->_context->mac)){
				$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
			}

			if(isset($this->_context->id)){
				$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
			}
			//print_r($achosts);
			//$this->_view['groupc']= $allhave;
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Hostgroup::find($adming)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			
			if($this->_view['pager']['record_count']>0 && $page=='')
					$page=1;

			$this->_view['countno'] = $this->_view['pager']['page_size']*$page-9;
			//$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
			//echo $this->_view['pager']['record_count'];
			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'behaviorlog|list|','\')',5);

			$this->_view['pagestr']= $pagediv->show();

		}else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
	}

	/*Host Manage*/
	function actionHostlist()
    {
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		$show_box['title']='行为日志-主机组';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

		$rightg='';$allhave2=array();
		//判断是否需要统计今天的数据
		$istoday=false;
		//判断是否跨越历史数据
		$iscross=false;
		$isfirst=true;
		
		//时间筛选
			$addconds="date = '".date("Y-m-d")."'";
			//关键字筛选，IP筛选
			$addkeys='';$addips='';
			$this->_view['startd']='';
			$this->_view['endd']='';

			//$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/hostlist'));
			if ($this->_context->isPOST() && isset($_POST['startdate']) && isset($_POST['enddate']))
			{
				$allccs = new Helper_Counter();
				$nowtt=$allccs->AllCount($_POST['startdate'],$_POST['enddate']);
				
				$this->_view['startd']=$_POST['startdate'].'';
				$this->_view['endd']=$_POST['enddate'].'';
				$addconds="time >='".$_POST['startdate']."' and time<='".$_POST['enddate']."'";

				$allhave2=$allccs->Counter_config($nowtt,$addconds);
			}
			$andstr='';
			if(isset($_POST['groupid']))
				$rightg=''.$_POST['groupid'].'';

			if(isset($_POST['searchtxt']) && $_POST['searchtxt']!='')
					$addkeys=' hostname like \'%'.$_POST['searchtxt'].'%\'';

				if(isset($_POST['searchip']) && $_POST['searchip']!=''){
					if($addkeys!='')$andstr=' and ';
					$addkeys.=$andstr.' ipaddr like \'%'.$_POST['searchip'].'%\'';
				}

			if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
				$enddstr=$this->my_date_add('d',date("Y-m-d"),1);
				
				$allccs = new Helper_Counter();
				$nowtt=$allccs->AllCount($this->_context->startd,$this->_context->endd);
				
				$this->_view['startd']=$this->_context->startd.'';
				$this->_view['endd']=$this->_context->endd.'';
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";

				$allhave2=$allccs->Counter_config($nowtt,$addconds);
			}else{
				if(!$this->_context->isPOST()){
					$allccs = new Helper_Counter();
					$addconds1="time >='".date("Y-m-d")." 00:00:00' and time<='".date("Y-m-d")." 23:59:59'";
					$allhave2=$allccs->Counter_config('1',$addconds1);
				}
			}
		
	
		//缓存主机组
		$table =QDB::getConn();
		$sql = "SELECT hostid, groupname FROM `group` JOIN `grouptohost` ON `grouptohost`.groupid = `group`.id";
			$handel =$table->execute($sql);
			$temp5 = $handel->fetchAll();
			$hgroups = array();
			foreach ($temp5 as $s){
				$hgroups[$s['hostid']]=array();
				array_push($hgroups[$s['hostid']],$s['groupname']);
				//echo $s['user_name'];
			}

		$this->_view['hostc']= $allhave2;
		$this->_view['hostg']= $hgroups;

		$show_box['title'] = '主机列表';
		// 设置页面列表数目
		//$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		//$page = $this->_context->page;
		//echo $page;
		//echo $this->_context->id;
		$allrit='';
		$this->_view['gid']=$this->_context->id;
		//$this->_view['pageno']=$page;
		if($this->_context->id!=0){
			// 查询指定用户 ID
			$edit_one = Hostgroup::find('id = ?', $this->_context->id )->query();
			if ($edit_one->groupname!='')
			{ // 如果用户 ID 无效，则返回用户管理首页
				$this->_view['gpname']=$edit_one->groupname;
			}
			
			$select = Host::find('[group.groupid]= ?',$this->_context->id);
			
		}
		else{
			//echo $addkeys;
			if($addkeys==''){
				if($rightg=='')
					$select = Host::find('grouptohost.groupid is null')->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id');
				else
					$select = Host::find('[group.groupid] in ('.$rightg.')');
			}else{
				if($rightg!='')
					$allrit=' and [group.groupid] in ('.$rightg.')';
				
				if(isset($_POST['searchip']) && $_POST['searchip']!='' && $_POST['searchtxt']=='')
					$select = Host::find('[group.groupid] is not null '.$allrit.' and ipaddr like ?','%'.$_POST['searchip'].'%');
				else if(isset($_POST['searchtxt']) && $_POST['searchtxt']!='' && $_POST['searchip']=='')
					$select = Host::find('[group.groupid] is not null '.$allrit.' and hostname like ?','%'.$_POST['searchtxt'].'%');
				else
					$select = Host::find('[group.groupid] is not null '.$allrit.' and ipaddr like ? and hostname like ?','%'.$_POST['searchip'].'%','%'.$_POST['searchtxt'].'%');

				//$select = Host::find($addkeys)->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id');
			}

			$this->_view['gpname']='';
		}
		//->limitPage($page, $pagesize);
		//$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		//$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
		$this->_view['countno'] = 1;
		//if($this->_view['pager']['record_count']>0 && $page=='')
			//$page=1;

		//echo $this->_view['pager']['record_count'];
		//$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'behaviorlog|hostlist|','\')',5);
		//$this->_view['pagestr']= $pagediv->show();
    }

	/* ******************************************************************** */
    function actionFliter()
	{
		$show_box['title'] = '过滤查询';
		
		if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

			$this->_view['form'] = new Form_BeFliter(url('default::behaviorlog/index'));
		}
	}
    
	function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}

	function DateDiff($d1,$d2)
	{
		//日期比较函数
		 if(is_string($d1))$d1=strtotime($d1);
		 if(is_string($d2))$d2=strtotime($d2);
		 return ($d2-$d1)/86400; //这里返回的是天数，如果除以3600就是返回小时了，依此类推
	}

}